package com.momo.mal.manga.view;

import net.momo.mal.R;
import com.momo.mal.database.MalDatabaseAdapter;
import com.momo.mal.entity.Manga;
import com.momo.mal.entity.Related;
import com.momo.mal.util.MangaUtil;


import org.apache.commons.lang3.text.WordUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MangaTabInformation extends Fragment {
	private JSONObject jsonObject;
	private String mContent = "???";
	
	private static final String KEY_CONTENT = "MangaTabInformation:Content";
	
	public static MangaTabInformation newInstance(JSONObject jsonObject) {
		MangaTabInformation fragment = new MangaTabInformation();
		fragment.jsonObject = jsonObject;
		
		return fragment;
	}
	

	@Override
	public void onPause() {
		super.onPause();
		getActivity().getIntent().putExtra("json", jsonObject.toString());
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(getActivity().getIntent().hasExtra("json")) {
			try {
				jsonObject = new JSONObject(getActivity().getIntent().getStringExtra("json"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(getActivity().getIntent().hasExtra("json")) {
			try {
				jsonObject = new JSONObject(getActivity().getIntent().getStringExtra("json"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.manga_view_information, null);
		
		MalDatabaseAdapter dbAdapter = new MalDatabaseAdapter(getActivity());
		
		Manga manga = new Manga();
		try {
			JSONObject json = jsonObject;
			if(json.has("sync") == true) {
				manga = dbAdapter.getMangaById(json.getString("manga_id"));
				Log.d("info", "1");
			} else {
				manga = MangaUtil.parsingMangaFromJson(json);
				Log.d("info", "2");
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		TextView type = (TextView) view.findViewById(R.id.text_type);
		TextView chapter = (TextView) view.findViewById(R.id.text_chapters);
		TextView volumes = (TextView) view.findViewById(R.id.text_volumes);
		TextView status = (TextView) view.findViewById(R.id.text_status);
		TextView genre = (TextView) view.findViewById(R.id.text_genre);
		TextView memberScore = (TextView) view.findViewById(R.id.text_member_score);
		TextView rank = (TextView) view.findViewById(R.id.text_rank);
		TextView popularity = (TextView) view.findViewById(R.id.text_popularity);
		TextView memberCount = (TextView) view.findViewById(R.id.text_member_count);
		TextView favorite = (TextView) view.findViewById(R.id.text_favorite);
		TextView english = (TextView) view.findViewById(R.id.text_english);
		TextView synonyms = (TextView) view.findViewById(R.id.text_synonyms);
		TextView japanese = (TextView) view.findViewById(R.id.text_japanese);
		TextView animeAdaptation = (TextView) view.findViewById(R.id.text_adaptation);
		TextView relatedManga = (TextView) view.findViewById(R.id.text_related_manga);
		TextView alternative = (TextView) view.findViewById(R.id.text_alternative);
		TextView score = (TextView) view.findViewById(R.id.text_score);
		TextView volumesRead = (TextView) view.findViewById(R.id.text_volumes_read);
		TextView chaptersRead = (TextView) view.findViewById(R.id.text_chpaters_read);
		TextView statusRead = (TextView) view.findViewById(R.id.text_status_read);
		
		TextView titleAlternativeTitle = (TextView) view.findViewById(R.id.title_alternative_title);
		TextView titleRelatedManga = (TextView) view.findViewById(R.id.title_related_manga);
		TextView titleMyInfo = (TextView) view.findViewById(R.id.title_my_info);
		
		type.setText("Type : " + manga.getType());
		chapter.setText("Chapters : " + manga.getChapters());
		volumes.setText("Volumes : " + manga.getVolumes());
		status.setText("Status : " + WordUtils.capitalize(manga.getStatus()));
		genre.setText("Genres : " + manga.getGenres());
		memberScore.setText("Member Score : " + manga.getMemberScore());
		rank.setText("Rank : #" + manga.getRank());
		popularity.setText("Popularity : #" + manga.getPopularityRank());
		memberCount.setText("Members : " + manga.getMemberCount());
		favorite.setText("Favourites : " + manga.getFavoriteCount());
		
		if(manga.getStatusSync() != null) {
			volumesRead.setText("Volumes seen : " + manga.getVolumesRead() + " / " + manga.getVolumes());
			chaptersRead.setText("Chapters seen : " + manga.getChapterRead() + " / " + manga.getChapters());
			statusRead.setText("Read status : " + MangaUtil.parsingReadStatusView(manga.getReadStatus()));
			if(manga.getScore().equals("10"))
				score.setText("Score : " + manga.getScore() + " - Masterpiece");
			else if(manga.getScore().equals("9"))
				score.setText("Score : " + manga.getScore() + " - Great");
			else if(manga.getScore().equals("8"))
				score.setText("Score : " + manga.getScore() + " - Very Good");
			else if(manga.getScore().equals("7"))
				score.setText("Score : " + manga.getScore() + " - Good");
			else if(manga.getScore().equals("6"))
				score.setText("Score : " + manga.getScore() + " - Fine");
			else if(manga.getScore().equals("5"))
				score.setText("Score : " + manga.getScore() + " - Average");
			else if(manga.getScore().equals("4"))
				score.setText("Score : " + manga.getScore() + " - Bad");
			else if(manga.getScore().equals("3"))
				score.setText("Score : " + manga.getScore() + " - Very Bad");
			else if(manga.getScore().equals("2"))
				score.setText("Score : " + manga.getScore() + " - Horrible");
			else if(manga.getScore().equals("1"))
				score.setText("Score : " + manga.getScore() + " - Appalling");
			else
				score.setText("Score : -");
		} else {
			volumesRead.setVisibility(TextView.GONE);
			chaptersRead.setVisibility(TextView.GONE);
			score.setVisibility(TextView.GONE);
			statusRead.setVisibility(TextView.GONE);
			titleMyInfo.setVisibility(TextView.GONE);
		}
		
		if(manga.getEnglish().length() == 0) {
			english.setVisibility(TextView.GONE);
		}
		if(manga.getJapanese().length() == 0) {
			japanese.setVisibility(TextView.GONE);
		}
		if(manga.getSynonyms().length() == 0) {
			synonyms.setVisibility(TextView.GONE);
		}
		if(english.getVisibility() == TextView.GONE && synonyms.getVisibility() == TextView.GONE && japanese.getVisibility() == TextView.GONE) {
			titleAlternativeTitle.setVisibility(TextView.GONE);
		}
		english.setText("English : " + manga.getEnglish());
		japanese.setText("Japanese : " + manga.getJapanese());
		synonyms.setText("Synonyms : " + manga.getSynonyms());
		
		if(manga.getAnimeAdaptation().length() == 0) {
			animeAdaptation.setVisibility(TextView.GONE);
		}
		if(manga.getRelatedManga().length() == 0) {
			relatedManga.setVisibility(TextView.GONE);
		}
		if(manga.getAlternative().length() == 0) {
			alternative.setVisibility(TextView.GONE);
		}
		if(animeAdaptation.getVisibility() == TextView.GONE && relatedManga.getVisibility() == TextView.GONE && alternative.getVisibility() == TextView.GONE) {
			titleRelatedManga.setVisibility(TextView.GONE);
		}
		
		String adapt = "Adaptation : ", related = "Related : ", alternatives = "Alternative : ";
		int intAdapt = 0, intRelated = 0, intAlternatives = 0;
		for(Related r : manga.getRelated()) {
			if(r.getType().equalsIgnoreCase("adaptations")) {
				if(intAdapt == 0) {
					adapt = adapt + "<a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				} else {
					adapt = adapt + ", <a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				}
				
				intAdapt++;
			}
			
			if(r.getType().equalsIgnoreCase("related_manga")) {
				if(intRelated == 0) {
					related = related + "<a href=\"manga://view?manga_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				} else {
					related = related + ", <a href=\"manga://view?manga_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				}
				
				intRelated++;
			}
			
			if(r.getType().equalsIgnoreCase("alternative_versions")) {
				if(intAlternatives == 0) {
					alternatives = alternatives + "<a href=\"manga://view?manga_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				} else {
					alternatives = alternatives + ", <a href=\"manga://view?manga_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				}
				
				intAlternatives++;
			}
		}
		
		animeAdaptation.setText(Html.fromHtml(adapt.toString()));
		animeAdaptation.setMovementMethod(LinkMovementMethod.getInstance());
		
		relatedManga.setText(Html.fromHtml(related.toString()));
		relatedManga.setMovementMethod(LinkMovementMethod.getInstance());
		
		alternative.setText(Html.fromHtml(alternatives.toString()));
		alternative.setMovementMethod(LinkMovementMethod.getInstance());
		
		return view;
		
	}
	
}
